package cn.ee.dfs.webservice;

import cn.ee.dfs.webservice.entity.CraftInfo;
import cn.ee.dfs.webservice.service.DfsWebService;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 该类提供两种不同的方式来调用webservice服务
 * 1：代理工厂方式
 * 2：动态调用webservice
 */
@Component
public class CxfClient {

    final static String address = "http://127.0.0.1:8080/ws/dfsWebService?wsdl";

    static {
        initProxyClient();
    }

    static DfsWebService dfsWebService;

    private static void initProxyClient() {
        // 接口地址
        // 代理工厂
        JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
        // 设置代理地址
        jaxWsProxyFactoryBean.setAddress(address);
        // 设置接口类型
        jaxWsProxyFactoryBean.setServiceClass(DfsWebService.class);
        // 创建一个代理接口实现
        dfsWebService = (DfsWebService) jaxWsProxyFactoryBean.create();
    }

    public static Object getCraftInfoList(int count) {
        return dfsWebService.getCraftInfoList(count);
    }

    public static Object getOrderInfoList(int count) {
        return dfsWebService.getOrderInfoList(count);
    }

    public static Object getTimeMillis() {
        return dfsWebService.getTimeMillis();
    }


    /**
     * 1.代理类工厂的方式,需要拿到对方的接口地址
     */
    public static Object main1() {
        try {
            // 接口地址
            // 代理工厂
            JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
            // 设置代理地址
            jaxWsProxyFactoryBean.setAddress(address);
            // 设置接口类型
            jaxWsProxyFactoryBean.setServiceClass(DfsWebService.class);
            // 创建一个代理接口实现
            DfsWebService us = (DfsWebService) jaxWsProxyFactoryBean.create();
            // 数据准备
            String userId = "zz";
            // 调用代理接口的方法调用并返回结果
            List<CraftInfo> craftInfoList = us.getCraftInfoList(10);
            System.out.println("返回结果:" + craftInfoList);
            return craftInfoList;
        } catch (Exception e) {
            e.printStackTrace();
            return "error:" + e.getMessage();
        }
    }

    /**
     * 2：动态调用
     */
    public static Object main2() {
        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(address);
        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
        Object[] objects = new Object[0];
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke("getOrderInfoList", 10);
            System.out.println("返回数据:" + objects[0]);
            return objects[0];

        } catch (java.lang.Exception e) {
            e.printStackTrace();
            return "error:" + e.getMessage();
        }
    }

    public static void main(String[] args) {
//        CxfClient.main1();
//        CxfClient.main2();
    }
}